home *** CD-ROM | disk | FTP | other *** search
- /* File: AppleEventHandlers.c */
- #include "AESimple.h"
-
- static char *OSTypeToPStr(OSType theType, char *s);
-
- void InstallAppleEventHandlers()
- {
- OSErr err;
- long result;
-
- err = Gestalt(gestaltAppleEventsAttr, &result);
- if (err == noErr) {
- (void)AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, (EventHandlerProcPtr)HandleOapp, 0, false);
- (void)AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, (EventHandlerProcPtr)HandleOdoc, 0, false);
- (void)AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, (EventHandlerProcPtr)HandlePdoc, 0, false);
- (void)AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, (EventHandlerProcPtr)HandleQuit, 0, false);
- /* The last handler we install is a "catch-all" for debugging */
- (void)AEInstallEventHandler(typeWildCard, typeWildCard, (EventHandlerProcPtr)NotHandled, 0, false);
- }
- } /* init_apple_events_hook */
-
-
- static char *OSTypeToPStr(OSType theType, char *s)
- /* This is a useful debugging tool as it allows us to put an EventID or EventID */
- /* into a string so we can display it in a dialog */
- {
- *s = 0x04;
- BlockMove(&theType, &s[1], 4); /* Use BlockMove to avoid odd address error on 68000 machines */
- return s;
- }
-
- pascal OSErr NotHandled (AEDescList *aevt, AEDescList *reply, long refCon)
- /* This routine is called for any unknown events that come in (it's installed into the */
- /* table with wild-card handlers) and it will put up a dialog that lets us know that a */
- /* "bad" Apple event has come in. */
- {
- OSErr err;
- OSType eventClass, eventID;
- OSType typeCode;
- char scratchClass[5], scratchID[5];
- Size actualSize;
- short itemHit;
-
- AEDesc directParam;
-
-
- err = MyInteractWithUser(TRUE); /* Tell the user it's urgent */
- if (err == noErr) {
- AEGetAttributePtr(aevt, keyEventClassAttr, typeType, &typeCode, (Ptr)&eventClass, sizeof(eventClass), &actualSize);
- AEGetAttributePtr(aevt, keyEventIDAttr, typeType, &typeCode, (Ptr)&eventID, sizeof(eventID), &actualSize);
- ParamText((void *)OSTypeToPStr(eventClass, scratchClass), (void *)OSTypeToPStr(eventID, scratchID), (void *)"", (void *)"");
- itemHit = Alert(1002, 0L);
- }
- return errAEEventNotHandled;
- } /* NotHandled */
-
-
- pascal OSErr HandleOapp (AEDescList *aevt, AEDescList *reply, long refCon)
- {
- NewDisplayWindow();
- return noErr;
- } /* NotHandled */
-
-
- pascal OSErr HandleOdoc (AEDescList *aevt, AEDescList *reply, long refCon)
- {
- AEDesc fileListDesc;
- long numFiles;
- DescType actualType;
- long actualSize;
- AEKeyword actualKeyword;
- FSSpec oneFile;
- long index;
- OSErr err;
-
- /* The "odoc" and "pdoc" messages contain a list of aliases as the direct paramater. */
- /* This means that we'll need to extract the list, count the list's elements, and */
- /* then process each file in turn. */
-
- /* Extract the list of aliases into fileListDesc */
- err = AEGetKeyDesc( aevt, keyDirectObject, typeAEList, &fileListDesc );
- if (err) return err;
-
- /* Count the list elements */
- err = AECountItems( &fileListDesc, &numFiles);
- if (err) return err;
-
- /* now get each file from the list and process it. */
- /* Even though the event contains a list of alises, the Apple Event Manager */
- /* will convert each alias to an FSSpec if we ask it to. */
- for (index = 1; index <= numFiles; index ++) {
- err = AEGetNthPtr( &fileListDesc, index, typeFSS, &actualKeyword,
- &actualType, (Ptr)&oneFile, sizeof(oneFile), &actualSize);
- if (err) {
- AEDisposeDesc(&fileListDesc );
- return err;
- }
-
- NewDisplayWindow();
- open_selected_file(&oneFile, FrontWindow());
- }
- return noErr;
- } /* HandleOdoc */
-
-
- pascal OSErr HandlePdoc (AEDescList *aevt, AEDescList *reply, long refCon)
- {
- AEDesc fileListDesc;
- long numFiles;
- DescType actualType;
- long actualSize;
- AEKeyword actualKeyword;
- FSSpec oneFile;
- long index;
- OSErr err;
-
- /* The "odoc" and "pdoc" messages contain a list of aliases as the direct paramater. */
- /* This means that we'll need to extract the list, count the list's elements, and */
- /* then process each file in turn. */
-
- /* Extract the list of aliases into fileListDesc */
- err = AEGetKeyDesc( aevt, keyDirectObject, typeAEList, &fileListDesc );
- if (err) return err;
-
- /* Count the list elements */
- err = AECountItems( &fileListDesc, &numFiles);
- if (err) return err;
-
- /* now get each file from the list and process it. */
- /* Even though the event contains a list of alises, the Apple Event Manager */
- /* will convert each alias to an FSSpec if we ask it to. */
- for (index = 1; index <= numFiles; index ++) {
- err = AEGetNthPtr( &fileListDesc, index, typeFSS, &actualKeyword,
- &actualType, (Ptr)&oneFile, sizeof(oneFile), &actualSize);
- if (err) {
- AEDisposeDesc(&fileListDesc );
- return err;
- }
-
- NewDisplayWindow();
- open_selected_file(&oneFile, FrontWindow());
- if (ShowJobDialog(FrontWindow()))
- PrintWindow(FrontWindow());
- CloseAWindow(FrontWindow());
- }
- return noErr;
- } /* HandlePdoc */
-
-
- pascal OSErr HandleQuit (AEDescList *aevt, AEDescList *reply, long refCon)
- {
- gDone = TRUE;
- return noErr;
- } /* NotHandled */
-